<?php

	function initIrc($nfo)
	{
		$irc = new irc($nfo['server'],$nfo['port'],$nfo['ssl']);
		$stream = $irc->getStream();
		$nick = new nick($nfo['nick'],$irc);
		$irc->sendRaw("USER {$nfo['nick']} {$nfo['ident']} {$nfo['server']} :{$nfo['realname']}");
                $nick->setIrcNick(); //this NICK comamnd
                $irc->sendRaw("JOIN {$nfo['chan']}");
	        $current = array('stream' => $stream, 'nick' => $nick, 'irc' => $irc);
                return $current;
	}

/*	function initIrcOld($server,$port,$nick,$ident,$realname,$chan)
	{
		
	        $irc = new irc ($server,$port);
	        $stream = $irc->getStream();
		$nick = new nick($nick,$irc);
		$irc->sendRaw("USER $nick $ident irc.freenode.org :$realname");
	        $irc->setIrcNick(); //this NICK comamnd
        	$irc->sendRaw('JOIN $chan');
		$current = array('stream' => $stream, 'nick' => $nick, 'irc' => $irc);
		return $current;
	}
*/
	//specical socket functions --> should really be moved to IRC static class
        function select($array, &$vkey, $timeout=0)
        {
                $select = array();
                $null = NULL;
                foreach($array as $key => $sock)
                {
                        $x = count($select);
                        $select[$x] = $sock;
                        $keys[$x] = $key;
                }
                if(stream_select($select, $null, $null, $timeout))
                {
                        foreach($keys as $key)
                        {
                                if($array[$key] == $select[0])
                                {
                                $vkey = $key;
                                return($select[0]);
                                }
                        }
                }
        }

	//special parse echo function --> should be made MUCH more efficient; perhaps by a getRawString thing from $cli
	//move to cli class static function? nonstatic?
        function echoMsg($txt)
        {
                global $cli,$whichCon;
                $txt = explode(' ',$txt,4);
                $cli->addCache($whichCon.' ');
                $cli->setColor(33);
                $cli->addCache($txt[0]. ' ',true);
                $cli->setColor(34);
                $cli->addCache($txt[1]. ' ',true);
                if (isset($txt[2]))
                {
                        $cli->setColor(35);
                        $cli->addCache($txt[2]. ' ',true);
                        if (isset($txt[3]))
                        {
                                $cli->setColor(36);
                                $cli->addCache($txt[3]. ' ',true);
                        }
                }
                $cli->setColor(0);
                $cli->addCache('',true);
                $cli->writeCache();
        }

        function charAt($str,$pos) {
                return (substr($str,$pos,1) !== false) ? substr($str,$pos,1) : -1;
        }
	
	function reHashModules($type)
	{
		return ls("./modules/$type");
		
	}

	function ls ($directory) 
	{

	    // create an array to hold directory list
	    $results = array();

	    // create a handler for the directory
	    $handler = opendir($directory);

	    // keep going until all files in directory have been read
		while ($file = readdir($handler))
		{

        		// if $file isn't this directory or its parent, 
	        	// add it to the results array
        		if ($file != '.' && $file != '..' && $file != '.svn')
	            	$results[] = $file;
	    	}

	    // tidy up: close the handler
	    closedir($handler);

	    // done!
	    return $results;

	}

        function tokenize($txt,&$tokens)
        {
                return str_replace($tokens[0],$tokens[1],$txt);
        }

	function puke($txt=null,$die=false)
	{
		echo $txt;
		if ($die) { die(); }
	}
	
?>
